package myactionplugin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import com.nomagic.magicdraw.ui.dialogs.MDDialogParentProvider;

import freemarker.template.Template;

/**
 * 使用freemarker模板转换java代码
* <p>Title:ChangeToJava </p>
* <p>Description: </p>
* <p>Company: </p> 
* @author 二胡
* @date 2018-10-8下午5:43:41
 */
public class ChangeToJava {

	public static void changeToJava(String a){
		
		//获取包名
		String packageName = ToJavaUtils.getPackage(a);
		//将包这一层去掉
		a = ToJavaUtils.getMainString(a);
		a=a.replace("{}", "");
		//将类名替换成java格式,再拆分
		String[] strings = ToJavaUtils.changeJava(a.trim());
		//遍历
		for(String string : strings){
			string = string.trim();
			if(!string.equals("")){
				//判断类的类别，进行拆分，封装成freemarker要使用的元数据map集合
				String classType = ToJavaUtils.getClassType(string);
				//如果是class
				if(classType.equals("class")){
					Map<String,Object> root  = new HashMap<String, Object>();
					root.put("classType", "class");
					root.put("packageName",packageName);
					//freemarker添加属性
	    			List<Map<String, String>> properties = new ArrayList<Map<String, String>>();
	    			root.put("properties", properties);
	    			//freemarker添加方法
	    			List<Map<String, String>> methods = new ArrayList<Map<String, String>>();
	    			root.put("methods", methods);
	    			
					//获取类名
					String className = ToJavaUtils.getClassName(string);
					root.put("className", className);
					//获取属性
					String mainString = ToJavaUtils.getMainString(string);
					String beforeSpliteMethod = null;
					String[] splits = mainString.split(";");
					//为分割前的属性字符串
					List<String> beforeSpliteAttr = new ArrayList<>();
					for(String s : splits){
						s=s.trim();
						if(s.indexOf("()")<0 && s.length()>0){
							System.out.println(s+"--"+s.length());
							s=s.replaceAll("\r|\n","");
							beforeSpliteAttr.add(s.trim());
						}
						if(s.indexOf("()")>-1){
							beforeSpliteMethod = s.trim();
						}
						
					}
					for(String s2 : beforeSpliteAttr){
						//freemarker添加属性
						Map<String, String> propertyame = new HashMap<String, String>();
						String[] split = s2.split(" ");
						propertyame.put("visibility",split[0]);
						propertyame.put("type",split[1]);
						propertyame.put("name",split[2]);
						properties.add(propertyame);
					}
					if(beforeSpliteMethod!=null){
						//获取方法
						beforeSpliteMethod = beforeSpliteMethod.replaceAll("\r|\n","");
						beforeSpliteMethod = beforeSpliteMethod.replace("()",";"); 
						String[] split = beforeSpliteMethod.split(";");
						//为分割前的方法字符串
						List<String> beforeSplitemeth = new ArrayList<>();
						for(String s : split){
							s=s.trim();
							beforeSplitemeth.add(s);
						}
						for(String s : beforeSplitemeth){
							//freemarker添加方法
							Map<String, String> methodame = new HashMap<String, String>();
							String[] split2 = s.split(" ");
							methodame.put("visibility",split2[0]);
							methodame.put("type",split2[1]);
							methodame.put("name",split2[2]);
							methods.add(methodame);
						}
					}
					
					//freemark转化模板
					Template template = FreemarkerUtil.getTemplate("fdl.txt");
					FreemarkerUtil.fprint(template, root, className+".java",packageName);
					
				}
				//如果是enum
				if(classType.equals("enum")){
					Map<String,Object> root  = new HashMap<String, Object>();
					root.put("classType", "enum");
					root.put("packageName",packageName);
	    			//freemarker添加枚举
	    			List<Map<String, String>> enumerations = new ArrayList<Map<String, String>>();
	    			root.put("enumerations", enumerations);
	    			//获取类名
					String className = ToJavaUtils.getClassName(string);
					root.put("className", className);
					
					String mainString = ToJavaUtils.getMainString(string);
					mainString = mainString.replaceAll("\r|\n","");
					mainString = mainString.trim();
					
					String[] split = mainString.split(",");
					List<String> enums = new ArrayList<>();
					for(String s : split){
						s=s.trim();
						Map<String, String> propertyname = new HashMap<String, String>();
						propertyname.put("enums", s);
						enumerations.add(propertyname);
					}
					
					//freemark转化模板
					Template template = FreemarkerUtil.getTemplate("fdl.txt");
					FreemarkerUtil.fprint(template, root, className+".java",packageName);
				}
				//如果是interface
				if(classType.equals("interface")){
					Map<String,Object> root  = new HashMap<String, Object>();
					root.put("classType", "interface");
					root.put("packageName",packageName);
	    			//freemarker添加方法
	    			List<Map<String, String>> methods = new ArrayList<Map<String, String>>();
	    			root.put("methods", methods);
	    			//获取类名
					String className = ToJavaUtils.getClassName(string);
					root.put("className", className);
					
					String mainString = ToJavaUtils.getMainString(string);
					mainString = mainString.replaceAll("\r|\n","");
					mainString = mainString.replace("()","");
					mainString = mainString.trim();
					
					List<String> beforeSpliteMeth = new ArrayList<>();
					String[] split = mainString.split(";");
					for(String s : split){
						beforeSpliteMeth.add(s.trim());
					}
					for(String s : beforeSpliteMeth){
						Map<String, String> methodame = new HashMap<String, String>();
						String[] splits = s.split(" ");
						methodame.put("visibility",splits[0]);
						methodame.put("type",splits[1]);
						methodame.put("name",splits[2]);
						methods.add(methodame);
					}
					
					//freemark转化模板
					Template template = FreemarkerUtil.getTemplate("fdl.txt");
					FreemarkerUtil.fprint(template, root, className+".java",packageName);
					
				}
			}
		}
		
		JOptionPane.showMessageDialog(MDDialogParentProvider.getProvider().getDialogParent(),"java代码已生成  d:\\"+packageName);
		
	}
	
}
